package code.rocky.consumer;

public class JoinTest3 {

    public static void main(String[] args) {
        Object object = new Object();
        MyThread mythread = new MyThread("mythread ", object);
        mythread.start();
        synchronized (mythread)
//        synchronized (object)
        {
            for (int i = 0; i < 20; i++) {
                if (i == 10) {
                    try {
                        System.out.println("开始join");
                        mythread.join();//main主线程让出CPU执行权，让mythread子线程优先执行
                        System.out.println("结束join");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() +"==" + i);
            }
        }
        System.out.println("main方法执行完毕");
    }
}


class MyThread extends Thread {
    private String name;
    private Object obj;

    public MyThread(String name, Object obj) {
        this.name = name;
        this.obj = obj;
    }

    @Override
    public void run() {
        synchronized (obj) {
            for (int i = 0; i < 20; i++) {
                System.out.println(name + i);
            }
        }
    }
}
